<?php
class UriModule extends BaseModel
{
    private $_table_uri  = "uri";
    private $_table_uri_pv  = "uri_pv";


    public function __construct($db_choose = "default"){
        parent::__construct($db_choose);
    }

    public function getUriList($where = array(),$field='*',$order='id desc',$offset = 0,$limit = 10) {
        $list = $this->table($this->_table_uri)->field($field)->where($where)->order($order)->limit($offset,$limit)->select();
        if (empty($list)) {
            $list = array();
        }
        return $list;
    }
    public function getUriCount($where = array()) {
        $count = $this->table($this->_table_uri)->where($where)->count();
        return $count;
    }
    public function getUriById($id){
        $where = array('id' => $id);
        return $this->table($this->_table_uri)->where($where)->find();
    }
    public function getUriByKey($key){
        $where = array('key' => $key);
        return $this->table($this->_table_uri)->where($where)->find();
    }
    public function addUri($data) {
        return $this->table($this->_table_uri)->add($data);
    }
    public function saveUri($id,$data) {
        if (empty($id)){
            return false;
        }
        $where['id'] = $id;
        return $this->table($this->_table_uri)->where($where)->save($data);
    }

    public function updateUriPv($key, $num=1){
        $sql = "update {$this->_table_uri} set pv = pv+{$num} where `key`='{$key}'";
        $res = $this->_db->exec($sql);
        if (empty($res)) {
            return false;
        }
        return true;
    }

    public function getUriPvList($where = array(),$field='*',$order='id desc',$offset = 0,$limit = 10) {
        $list = $this->table($this->_table_uri_pv)->field($field)->where($where)->order($order)->limit($offset,$limit)->select();
        if (empty($list)) {
            $list = array();
        }
        return $list;
    }
    public function addUriPv($data) {
        return $this->table($this->_table_uri_pv)->add($data);
    }
}
